package com.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mall.common.page.PageData;
import com.mall.common.service.impl.CrudServiceImpl;
import com.mall.common.utils.ConvertUtils;
import com.mall.ware.dao.WareInfoDao;
import com.mall.ware.dto.WareInfoDTO;
import com.mall.ware.entity.WareInfoEntity;
import com.mall.ware.service.WareInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 仓库信息
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@Service
public class WareInfoServiceImpl extends CrudServiceImpl<WareInfoDao, WareInfoEntity, WareInfoDTO> implements WareInfoService {

    @Override
    public QueryWrapper<WareInfoEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public PageData<WareInfoDTO> queryPageByCondition(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(key), "id", key)
                .or().like(StringUtils.isNotBlank(key),"name",key)
                .or().like(StringUtils.isNotBlank(key),"address",key);
        IPage<WareInfoEntity> selectPage = baseDao.selectPage(
                getPage(params, null, false),
                wrapper);
        return new PageData<>(ConvertUtils.sourceToTarget(selectPage.getRecords(),WareInfoDTO.class),selectPage.getTotal());
    }
}